package com.shanpin.shebao.admin.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.shanpin.shebao.admin.exception.ParamParserException;
import com.shanpin.shebao.common.URI;
import com.shanpin.shebao.constant.QueryOperation;
import com.shanpin.shebao.form.CouponQueryForm;
import com.shanpin.shebao.model.Coupon;
import com.shanpin.shebao.service.CouponService;
import com.shanpin.shebao.utility.Criterition;
import com.shanpin.shebao.utility.MoreCriteria;
import com.shanpin.shebao.utility.PageModel;

/**
 * 现金券管理控制器
 * @author 品讯科技
 *
 */
@Controller
public class CouponAdminController extends ResourceController {
	
	@Autowired
	private CouponService couponService;
	
	/**
	 * 现金券管理查询列表
	 * @param request
	 * @param response
	 * @param form
	 * @return
	 */
	@RequestMapping(value=URI.COUPON_LIST, method=RequestMethod.GET)
	public Object queryPageBy(HttpServletRequest request, HttpServletResponse response, CouponQueryForm form) {
		
		MoreCriteria<String, Criterition> mc = new MoreCriteria<String, Criterition>();
		Map<String, Criterition> pair = (Map<String, Criterition>) mc.getPair();
		if(form.getName() != null && !"".equals(form.getName())) {
			Criterition c = new Criterition("coupon.name", form.getName().trim(), QueryOperation.EQ, QueryOperation.AND);
			pair.put("name", c);
		}
		
		if(form.getStatus() != null && !"".equals(form.getStatus())) {
			Criterition c = new Criterition("coupon.status", form.getStatus().trim(), QueryOperation.EQ, QueryOperation.AND);
			pair.put("status", c);
		}
		
		if(form.getStart_date() != null && !"".equals(form.getStart_date())) {
			Criterition c = new Criterition("coupon.start_date", form.getStart_date().trim(), QueryOperation.GE, QueryOperation.AND);
			pair.put("start_date", c);
		}
		
		if(form.getEnd_date() != null && !"".equals(form.getEnd_date())) {
			Criterition c = new Criterition("coupon.end_date", form.getEnd_date().trim() + " 23:59:59", QueryOperation.LE, QueryOperation.AND);
			pair.put("end_date", c);
		}
		
		if(form.getType() != null && !"".equals(form.getType())) {
			Criterition c = new Criterition("coupon.type", form.getType().trim(), QueryOperation.EQ, QueryOperation.AND);
			pair.put("type", c);
		}
		
		if(form.getInit_number() != null && !"".equals(form.getInit_number())) {
			Criterition c = new Criterition("coupon.init_number", form.getInit_number().trim(), QueryOperation.EQ, QueryOperation.AND);
			pair.put("init_number", c);
		}
		
		if(form.getPrice() != null && !"".equals(form.getPrice())) {
			Criterition c = new Criterition("coupon.price", form.getPrice().trim(), QueryOperation.EQ, QueryOperation.AND);
			pair.put("price", c);
		}
		
		if(form.getMonth_limit() != null && !"".equals(form.getMonth_limit())) {
			Criterition c = new Criterition("coupon.month_limit", form.getMonth_limit().trim(), QueryOperation.EQ, QueryOperation.AND);
			pair.put("month_limit", c);
		}
		
		
		int pageSize = PAGE_SIZE;
		
		int page = parsePageNo(form.getPageNo());
		
		PageModel pageModel = couponService.queryPage(page, pageSize, mc);
		request.setAttribute("pageModel", pageModel);
		request.setAttribute("couponQueryForm", form);

		request.setAttribute("coupon_user_list_uri", URI.SBA + URI.COUPON_USER_LIST);	
		request.setAttribute("coupon_list_uri", URI.SBA + URI.COUPON_LIST);	
		request.setAttribute("coupon_channel_list_uri", URI.SBA + URI.COUPON_LIST_CHANNEL);	
		request.setAttribute("coupon_link_list_uri", URI.SBA + URI.COUPON_LIST_LINK);	
		request.setAttribute("new_uri", URI.SBA + URI.COUPON_NEW);	
		request.setAttribute("edit_status_uri", URI.SBA + URI.COUPON_EDIT_STATUS);	
		request.setAttribute("channel_tobind_uri", URI.SBA + URI.COUPON_EDIT_CHANNEL_TOBIND);		
		request.setAttribute("coupon", true);
		request.setAttribute("coupon_channel", false);
		request.setAttribute("coupon_link", false);
		
		return "coupon_manage";
	}
	
	/**
	 * 添加现金券
	 * @param request
	 * @param response
	 * @param form
	 * @return
	 */
	@RequestMapping(value=URI.COUPON_NEW, method=RequestMethod.POST)
	public Object add(HttpServletRequest request, HttpServletResponse response, CouponQueryForm form) {
		
		if(form != null) {
			
			checkForm(form);
			
			Coupon coupon = new Coupon();
			coupon.setBind_number(0);
			coupon.setCreate_time(new Date());
			coupon.setDescription(form.getDescription());
			if(form.getEnd_date() != null && !"".equals(form.getEnd_date())) {
				try {
					coupon.setEnd_date(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(form.getEnd_date() + " 23:59:59"));
				} catch (ParseException e) {
					throw new ParamParserException("日期格式不正确");
				}
			}
			
			coupon.setInit_number(Integer.parseInt(form.getInit_number()));
			coupon.setName(form.getName());
			coupon.setPerson_limit(Integer.parseInt(form.getPerson_limit()));
			if(form.getMonth_limit() != null && !"".equals(form.getMonth_limit())) {
				int month = Integer.parseInt(form.getMonth_limit());
				coupon.setMonth_limit(month);
			}
			
			coupon.setPrice(Integer.parseInt(form.getPrice()));
			coupon.setSend_number(0);
			if(form.getStart_date() != null && !"".equals(form.getStart_date())) {
				try {
					coupon.setStart_date(new SimpleDateFormat("yyyy-MM-dd").parse(form.getStart_date()));
				} catch (ParseException e) {
					throw new ParamParserException("日期格式不正确");
				}
			}
			coupon.setStatus(Integer.parseInt(form.getStatus()));
			coupon.setType(Integer.parseInt(form.getType()));
			coupon.setUsed_number(0);
			
			if(coupon != null) {
				couponService.add(coupon);
			}
		}

		return "redirect:"+URI.SBA + URI.COUPON_LIST+"?pageNo=" + form.getPageNo();
	}
	
	private void checkForm(CouponQueryForm form) {
		if(form.getName() == null || "".equals(form.getName())) {
			throw new ParamParserException("现金券名称不能为空");
		}
		
		if(form.getType() == null || "".equals(form.getType())) {
			throw new ParamParserException("请选择现金券类型");
		}
		
		if(form.getStatus() == null || "".equals(form.getStatus())) {
			throw new ParamParserException("请选择现金券状态");
		}
		
		if(form.getPrice() == null || "".equals(form.getPrice())) {
			throw new ParamParserException("现金券面值金额不能为空");
		}
		
		if(form.getInit_number() == null || "".equals(form.getInit_number())) {
			throw new ParamParserException("初始化数量不能为空");
		}
		
		if(form.getPerson_limit() == null || "".equals(form.getPerson_limit())) {
			throw new ParamParserException("单用户领取限额不能为空");
		}
		
	}

	/**
	 * 绑定现金券查询列表
	 * @param request
	 * @param response
	 * @param form
	 * @return
	 */
	@RequestMapping(value=URI.COUPON_LIST_TO_BIND, method=RequestMethod.GET)
	public Object toBind(HttpServletRequest request, HttpServletResponse response, CouponQueryForm form) {
		
		MoreCriteria<String, Criterition> mc = new MoreCriteria<String, Criterition>();
		Map<String, Criterition> pair = (Map<String, Criterition>) mc.getPair();
		if(form.getName() != null && !"".equals(form.getName())) {
			Criterition c = new Criterition("coupon.name", form.getName().trim(), QueryOperation.EQ, QueryOperation.AND);
			pair.put("name", c);
		}
		
		int pageSize = PAGE_SIZE;
		
		int page = parsePageNo(form.getPageNo());
		
		PageModel pageModel = couponService.queryPage(page, pageSize, mc);
		request.setAttribute("pageModel", pageModel);
		request.setAttribute("couponQueryForm", form);
		request.setAttribute("bind_uri", URI.SBA + URI.COUPON_USER_EDIT_BIND);
		return "coupon_tobind";
	}
	
	/**
	 * 更改现金券状态
	 * @param request
	 * @param response
	 * @param form
	 * @return
	 */
	@RequestMapping(value=URI.COUPON_EDIT_STATUS, method=RequestMethod.GET)
	public Object updateStatus(HttpServletRequest request, HttpServletResponse response, CouponQueryForm form) {
		
		if(form.getCoupon_id() != null && !"".equals(form.getCoupon_id())) {
			
			Coupon coupon = couponService.queryBy(form.getCoupon_id());
		
			if(coupon != null) {
				coupon.setStatus(Integer.parseInt(form.getStatus()));
				couponService.updateStatus(coupon);
				
			}
			
		}
		
		return "redirect:"+URI.SBA + URI.COUPON_LIST+"?pageNo=" + form.getPageNo();
	}
	
}
